﻿///************** google maps API scripts *****************/
var gmMap; // The global map constant.
var gmLAT = 39.75; // the global latitude (defaults to healthgrades location).
var gmLONG = -105; // the global longitude (defaults to healthgrades location).
var ZOOM_INIT = 14; // the global zoom amount.
var Markers = null;
var CurrentData = null;
var InfoWindows = null;

$(document).ready(function () {
    Initialize();
    GetMapData();

        $("#ad_space_map").mouseenter(function () {
            $('#ad_space_map').stop(true, true).animate({
                height: '+=300'
            }, 400, function () {

            });
            $('#ad_space_map_additional').fadeIn('fast');
           

        });

        $("#ad_space_map").mouseleave(function () {
            $('#ad_space_map').stop(true, true).animate({
                height: '-=300'
            }, 400, function () {

            });
            $('#ad_space_map_additional').fadeOut('fast');

        });

        $("#SearchDropdown").DynamicDropdownPlugin({
            HeaderDefaultText: "Text Search",
            HeaderPreText: "Search: ",
            HeaderValueFieldId: "SearchInput",
            Width:"200px",
            OnDropdownChanged: function (item) {

            }
        });

        $('#FacilityTypeDropdown').DynamicDropdownPlugin({
            HeaderDefaultText: "Type of Location",
            HeaderPreText: "Location Type: ",
            DropdownMode: "Select",
            Width:"200px",
            OnDropdownChanged: function (item) {

            }
        });

        $('#RankDropdown').DynamicDropdownPlugin({
            HeaderDefaultText: "Dispensery Rating",
            HeaderPreText: "Rating: ",
            DropdownMode: "Select",
            Width: "200px",
            OnDropdownChanged: function (item) {

            }
        });
});

function GetMapData() {
    var serviceUrl = "../service/cService.ashx?handler=LocationHandler&action=GetLocationPoints";
    $.ajax({
        url: serviceUrl,
        dataType: 'json',
        beforeSend: function () {
            // Handle the beforeSend event
        },
        success: GetMapDataSuccess,
        error: GetMapDataFailure
    }); // $.ajax
}

function GetMapDataFailure(XMLHttpRequest, textStatus, errorThrown) {
    alert("failure")
}

function GetMapDataSuccess(data, textStatus, xmlRequest) {
    CurrentData = new Array();
    CurrentData = data;
    for (var i = 0; i < data.length; i++) {
        data[i].ItemIndex = i + 1;
        AddMarkerPoint(data[i]);
    }

    $("#ResultTemplate").tmpl(data).appendTo("#side_results_container");

    $(".actionButton").click(function (e) {
        e.preventDefault();
        var itemIndex = $(this).attr("itemindex") - 1;
        google.maps.event.trigger(Markers[itemIndex], 'click');
    });
}

function Initialize() {
    var mapOptions = {
        zoom: ZOOM_INIT,
        center: new google.maps.LatLng(gmLAT, gmLONG),
        panControl: false,
        zoomControl: true,
        zoomControlOptions: {
          style: google.maps.ZoomControlStyle.SMALL
        },
        scaleControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    gmMap = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);   
}

function AddMarkerPoint(data) {
    var latlng = new google.maps.LatLng(data.Latitude, data.Longitude);

   /* var image = new google.maps.MarkerImage(
    '../images/leafIconSmall.png',
    new google.maps.Size(32, 32),
    new google.maps.Point(0, 0),
    new google.maps.Point(16, 32)
    ); */

    var shadow = new google.maps.MarkerImage(
    '../images/shadow.png',
    new google.maps.Size(52, 32),
    new google.maps.Point(0, 0),
    new google.maps.Point(16, 32)
    );

    var shape = {
        coord: [19, 0, 20, 1, 21, 2, 22, 3, 23, 4, 24, 5, 24, 6, 24, 7, 24, 8, 24, 9, 24, 10, 24, 11, 24, 12, 23, 13, 23, 14, 22, 15, 21, 16, 20, 17, 20, 18, 19, 19, 19, 20, 18, 21, 18, 22, 17, 23, 17, 24, 17, 25, 17, 26, 16, 27, 16, 28, 16, 29, 16, 30, 16, 31, 14, 31, 14, 30, 14, 29, 14, 28, 14, 27, 14, 26, 13, 25, 13, 24, 13, 23, 12, 22, 12, 21, 12, 20, 11, 19, 10, 18, 10, 17, 9, 16, 8, 15, 7, 14, 7, 13, 6, 12, 6, 11, 6, 10, 6, 9, 6, 8, 6, 7, 6, 6, 7, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 19, 0],
        type: 'poly'
    };

    var marker = new google.maps.Marker({
        draggable: true,
        raiseOnDrag: false,
        //icon: image,
        shadow: shadow,
        shape: shape,
        map: gmMap,
        position: latlng
    });

    if (Markers == null) {
        Markers = new Array();
    }

    var contentString = '<div id="content">' +
    data.LocationName +
     "<br />Here is some div content" +
    '</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });
    if(InfoWindows == null) { 
        InfoWindows = new Array();
    }
    InfoWindows.push(infowindow);


    google.maps.event.addListener(marker, "click", function () {
        for(var i = 0; i < InfoWindows.length; i++)
        {
            InfoWindows[i].close();
        }
        infowindow.open(gmMap, marker);
        UpdateMaterial(data.ItemIndex - 1);
    });

    Markers.push(marker);
}

function UpdateMaterial(index) {
    if (!$("#top_panel").is(":visible")) {
        $('#top_panel').slideDown();
    }
    $(".HeaderText").text(CurrentData[index].LocationName);
}